package com.nkym.service.impl;/*
 * @ClassName InnerInterfaceInfoServiceImpl
 * @Desc TODO
 * @Author 19637
 * @Date 2023/4/25 15:20
 * @Version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nkym.common.ErrorCode;
import com.nkym.exception.BusinessException;
import com.nkym.pojo.entity.InterfaceInfo;
import com.nkym.pojo.entity.UserInterface;
import com.nkym.pojo.service.InnerInterfaceInfoService;
import com.nkym.service.InterfaceInfoService;
import com.nkym.service.UserInterfaceService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@DubboService
public class InnerInterfaceInfoServiceImpl implements InnerInterfaceInfoService {
    @Resource
    InterfaceInfoService interfaceInfoService;

    @Resource
    UserInterfaceService userInterfaceService;

    @Override
    public void decreaseInvokeTimes(Integer userId, Integer interfaceInfoId) {
        UpdateWrapper<UserInterface> wrapper = new UpdateWrapper<>();
        wrapper.eq("userId", userId);
        wrapper.eq("interfaceInfoId", interfaceInfoId);
        wrapper.setSql("leftNum = leftNum-1 , totalNum = totalNum+1");
        userInterfaceService.update(wrapper);
    }

    @Override
    public Integer getInterfaceInfoId(String method, String path) {
        Integer InterfaceInfoId = interfaceInfoService.getInterfaceInfoId(method, path);
        return InterfaceInfoId;
    }

    @Override
    public Integer hasInvokeTime(Integer userId, Integer interfaceInfoId) {
        QueryWrapper<UserInterface> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        wrapper.eq("interfaceInfoId", interfaceInfoId);
        wrapper.select("leftNum");
        UserInterface one = userInterfaceService.getOne(wrapper);
        if (one == null) return null;
        return one.getLeftNum();
    }
}
